﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OutlookInterop = Microsoft.Office.Interop.Outlook;
using System.Data;
using System.Runtime.InteropServices;

namespace xtraSMSCore
{
    public class Outlook
    {
        public DataTable getContacts()
        {
            OutlookInterop.Application OutlookApp = new OutlookInterop.Application();
            OutlookInterop.Folder contacts = (OutlookInterop.Folder)OutlookApp.Session.GetDefaultFolder(OutlookInterop.OlDefaultFolders.olFolderContacts);

            DataTable contactlist = new DataTable();
            contactlist.Columns.Add("Nachname", typeof(string));
            contactlist.Columns.Add("Vorname", typeof(string));
            contactlist.Columns.Add("Natelnummer", typeof(string));

            foreach (OutlookInterop.ContactItem foundContact in contacts.Items)
            {
                if (!string.IsNullOrEmpty(foundContact.MobileTelephoneNumber))
                {
                    if (!string.IsNullOrEmpty(foundContact.LastName) && !string.IsNullOrEmpty(foundContact.FirstName))
                    {
                        contactlist.Rows.Add(foundContact.LastName, foundContact.FirstName, foundContact.MobileTelephoneNumber);
                    }
                }
                Marshal.ReleaseComObject(foundContact);
            }
            Marshal.ReleaseComObject(contacts);
            Marshal.ReleaseComObject(OutlookApp);
            OutlookApp = null;
            return contactlist;
        }
    }
}